home *** CD-ROM | disk | FTP | other *** search
- Path: news.bridge.net!news
- From: psycho@bridge.net (Gary Thompson)
- Newsgroups: comp.lang.c
- Subject: Re: NEwbie: How to return a multi-dimensional array from function?
- Date: Fri, 15 Mar 1996 07:44:42 GMT
- Organization: A poorly-installed InterNetNews site
- Message-ID: <4ib78s$6gv@news.bridge.net>
- References: <4hp273$8bu@news.xs4all.nl> <31404CE9.1A4A@mc.net> <mjs.826298629@hubcap>
- NNTP-Posting-Host: ppp-ftl1-28.bridge.net
- X-Newsreader: Forte Free Agent 1.0.81
-
- mjs@hubcap.clemson.edu (M. J. Saltzman) wrote:
-
- >Sean Park <spark@mc.net> writes:
-
- >>Anthony Moendir wrote:
- >>>
- >>> I want to return a multidimensional array from a function,
- >>> something like this:
- >>>
- >>> char *Foo();
- >>> int main()
- >>> {
- >>> char tmp[10][5];
- >>> tmp=Foo();
- >>> }
- >>>
- >>> char *Foo()
- >>> {
- >>> char tmp[10][5];
-
- >Two things: (1) If you are going to use the value of tmp outside of Foo(),
- >then you need to declare
- > static tmp[10][5];
- >Otherwise the storage may disappear when you return from Foo().
-
- No, declaring it as static will only retain the value in the foo() function, if
- you call it more than once. You CANNOT use a value declared in a subfunction in
- MAIN. You would have to remove both TMP declarations from MAIN and FOO and
- declare the thing globally outside of main.
-
- >>> //do something
- >>> return(tmp);
- > ^ ^ Parentheses not required here.
- >>> }
-
- Makes no difference. They are compiled the same either way. Who would care
- about saving two bytes code space?
-
- >(2) When you use tmp in an expression, the compiler replaces the array
- >name with a constant pointer to its first element. (Among other
- >consequences, this means that arrays can't appear on the left side of
- >assignments.) In this case, the first element has type
- >array-of-15-chars, so a pointer to it is a
- >pointer-to-array-of-15-chars. That's the type that the function
- >should return, and the type of the variable that you assign it to.
- >Thus, Foo() needs to be declared
-
- > char (*Foo())[5];
- >and the tmp in main() should be
- > char (*tmp)[5];
-
- WHAT??? You just declared five FUNCTION pointers pointed to nothing! If you
- ran that, you'd get a NULL pointer error. It wouldn't even run! Again,
- declaring char (*tmp)[5] is declaring an array of five pointers to chars and
- assigning them to nothing... which will give you a NULL POINTER error AGAIN.
-
- >>> How should i do that?
- >>>
- >>> Any help would be appreciated
- >>> Anthony
-
- >Another technique for getting the result back is to pass the array in as an
- >argument, as is done below (with corrections).
-
- >>You could pass the pointer to the array to the function. That way the
- >>function itself will fill your array addresses rather than returning
- >>redundant data. You might try:
-
- >>void foo(char *tmp);
- > ^^^^^^^^^ Here, tmp should be declared char (*tmp)[5].
-
- That wont work. All pointers, require either 2 or 4 bytes (I forget which).
- You cannot pass data this way. Chars only take up one byte. If you pass an
- array of chars this way, you will get scrambled data at the other end. It will
- try to interpret the passed chars as pointer addresses. You will have to pass
- an array of POINTERS for this to work. And you cannot pass arrays of anything.
-
- >>int main()
- >>{
- >> char tmp[10][5];
- >> foo(tmp);
- >> return 0;
- >>}
- >>void foo(char *tmp)
- > ^^^^^^^^^ Here, tmp should be declared char (*tmp)[5].
-
- Ok, since you declared foo() to accept only an array of five pointers at 2 bytes
- each, why are you passing it a single pointer? When you pass it like foo(tmp),
- you are only passing it a single pointer to the beginning of the 2d array. You
- will get one pointer and the rest garbage.
-
- Besides, you are ignoring the question. You are passing the data TO foo, he
- wants to return it from FOO. Make it simple... There are several ways of doing
- it easy...
- --------------------
- void foo(void);
- char tmp[10][5];
-
- void main(void)
- {
- foo();
- /* do something else with tmp */
- }
-
- void foo(void)
- {
- /* do something with tmp */
- }
- -----------------
- That example allows you to access TMP within both main and foo. Problem is,
- it's not very good with security... tmp can be modified by ALL functions....
- not recommended if it's something critical.... This is a little more complicated
- but still easy...
- -----------------
- void foo(char *);
- main()
- {
- char tmp[10][5];
- foo(tmp);
- }
-
- char *foo(char *tmp2)
- {
- /* You can access tmp2 exactly like tmp... tmp2[10][5]...etc */
- }
- --------------------
- There are a few quirks with this also... tmp2 is accessed exactly like tmp
- because it is the same thing. tmp and tmp2 both access the same space. That's
- why you don't have to return anything. You can use tmp instead of tmp2, but it
- makes no difference. tmp in main is a different variable than tmp in foo. I
- like to use different variable names for the sake of clarity.
-
- One problem with C is that the ONLY way you can pass the data in an array
- without passing just the pointer is by using a STRUCT. passing the name of a
- struct is the only thing that will pass the data and not a pointer to the data.
- If you truly want to keep your variables independant, you will have to use a
- struct.
-
- That is... unless you want to program in C++... you can then use the first
- example and declare which functions have permission to modify it.
-
- Gary Thompson
- "The Psycho"
- psycho@bridge.net
- 72607.1365@compuserve.com
- http://www.bridge.net/~psycho
- HTTP://ourworld.compuserve.com/homepages/psychotps
-
-